package buffer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.jupiter.api.Test;

public class FileStreamDemo {
	@Test
	public void write() throws IOException {
		//1.创建字节输出流
		FileOutputStream fos = new FileOutputStream("names.txt",true);
		//2.创建带缓存区的输出字节流
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		//3.写
		byte[] content = "hello tom and jerry".getBytes();
		bos.write(content);
		//4.刷新
		bos.flush();
		//5.关闭
		bos.close();
		fos.close();
	}
	
	@Test
	public void read() throws IOException {
		//1.创建字节输入流
		FileInputStream fis = new FileInputStream("names.txt");
		//2.创建带缓存区的输入字节流
		BufferedInputStream bis = new BufferedInputStream(fis);
		//2.读取
		byte[] content = new byte[fis.available()];  //fis.available() 返回文件流里可用的字节数
		bis.read(content);
		//3.关闭
		bis.close();
		fis.close();
		
		System.out.println(new String(content));
	}
}
